package com.lucfzy.rest.controller.es;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.lucfzy.rest.dto.es.QueryParam;
import com.lucfzy.rest.dto.es.response.EsLogResultDTO;
import com.lucfzy.rest.dto.es.response.Hits;
import com.lucfzy.rest.dto.es.response.Response;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 基于es的app日志系统查询
 *
 * @author lucfzy
 * @ClassName EsApplogQueryController
 * @description 基于es的app日志系统查询
 * @created 2021-07-24 20:16
 **/
@CrossOrigin
@RestController
@RequestMapping("/es")
public class EsApplogQueryController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/log/suspect/query")
    public List<Response> suspectQuery(@NotBlank @RequestParam("keyword") String keyword) {
        // 组装请求体
        HttpEntity<String> reqBody = getReqBody(keyword);
        // 查询es记录
        ResponseEntity<EsLogResultDTO> exchange = getEsLogResultDTOResponseEntity(reqBody);
        // 处理响应结果
        return getResponses(exchange);
    }

    private ResponseEntity<EsLogResultDTO> getEsLogResultDTOResponseEntity(HttpEntity<String> reqBody) {
        return restTemplate.exchange("http://localhost:9200/logs/applog/_search", HttpMethod.GET, reqBody, EsLogResultDTO.class);
    }

    /**
     * 处理获得响应结果
     *
     * @param exchange 响应报文
     * @return java.util.List<com.lucfzy.rest.dto.es.response.Response>
     * @Author liu-miss/lucfz
     * @Description 处理获得响应结果
     * @Date 2021/7/25 8:50
     **/
    private List<Response> getResponses(ResponseEntity<EsLogResultDTO> exchange) {
        EsLogResultDTO body = exchange.getBody();
        if (CollectionUtils.isEmpty(body.getHits().getHits())) {
            // 返回一个空的对象
            return Lists.newArrayList(Response.builder().build());
        }
        List<Hits> hits = body.getHits().getHits();
        List<Response> responses = Lists.newLinkedList();
        for (int i = 0; i < hits.size(); i++) {
            Response response = Response.builder().build();
            response.setId(i + 1);
            response.setData(hits.get(i));
            responses.add(response);
        }
        return responses;
    }

    /**
     * 处理入参报文参数
     *
     * @param keyword 关键字入参
     * @return org.springframework.http.HttpEntity<java.lang.String>
     * @Author liu-miss/lucfz
     * @Description 处理入参报文参数
     * @Date 2021/7/25 8:51
     **/
    private HttpEntity<String> getReqBody(String keyword) {
        QueryParam.Match match = QueryParam.Match.builder().msg(keyword).build();
        QueryParam.Query query = QueryParam.Query.builder().match(match).build();
        QueryParam inputDTO = QueryParam.builder().query(query).build();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(JSON.toJSONString(inputDTO), headers);
    }

}